using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Storage;
using Microsoft.Xna.Framework.Content;
using BriansClimb.GameObjects;

namespace BriansClimb.DrawableGameObjects.Buttons
{
    /// <summary>
    /// This is a game component that creates the Left-Click Buttons
    /// </summary>
    public class LeftClickButton : DrawableGameComponent
    {
        protected Game game;
        protected SpriteBatch spritebatch;
        protected AudioManager audioManager;
        protected MouseState mouseState;
        protected Texture2D initialButtonTexture;

        protected int xCoord;
        protected int yCoord;
        protected bool clicked;
        protected bool pressed;
        protected string buttonName;

        /// <summary>
        /// Default Constructor
        /// </summary>
        /// <param name="game">Main Game Object</param>
        /// <param name="saveButton">Initial Save Button Texture</param>
        /// <param name="pressedSaveButton">Pressed State Texture of Save Button</param>
        /// <param name="x">Top Left Corner X-Coordinate of Texture</param>
        /// <param name="y">Top Left Corner Y-Coordinate of Texture</param>
        public LeftClickButton(Game game, string buttonName, int x, int y)
            : base(game)
        {
            this.game = game;
            this.buttonName = buttonName;
            xCoord = x;
            yCoord = y;

            //get current spritebatch and audio manager
            spritebatch = (SpriteBatch)Game.Services.GetService(typeof(SpriteBatch));
            audioManager = (AudioManager)Game.Services.GetService(typeof(AudioManager));

            LoadTextures();
        }

        /// <summary>
        /// Loads button textures based on the type of button to be created.
        /// </summary>
        private void LoadTextures()
        {
            switch (buttonName)
            {
                case "new":
                    initialButtonTexture = game.Content.Load<Texture2D>(".\\Textures\\NewButton");
                    break;
                case "next":
                    initialButtonTexture = game.Content.Load<Texture2D>(".\\Textures\\NextButton");
                    break;
                case "back":
                    initialButtonTexture = game.Content.Load<Texture2D>(".\\Textures\\BackButton");
                    break;
            }
        }

        /// <summary>
        /// Returns true if button has been clicked, false otherwise.
        /// </summary>
        public bool LeftButtonClick
        {
            get { return clicked; }
            set { clicked = value; }
        }

        /// <summary>
        /// Check for left mouse button click(press & release).
        /// </summary>
        private void CheckButtonClick()
        {
            mouseState = Mouse.GetState();


            if (mouseState.LeftButton == ButtonState.Pressed)
                pressed = true;
            if (mouseState.LeftButton == ButtonState.Released && pressed)
            {
                
                clicked = true;
                pressed = false;
            }
        }

        /// <summary>
        /// Checks if mouse is on the button location.
        /// </summary>
        /// <returns>True is on the button location, false otherwise.</returns>
        private bool MouseOnButton()
        {
            mouseState = Mouse.GetState();

            bool result = ((mouseState.X > xCoord) && (mouseState.X < (xCoord + initialButtonTexture.Width)) &&
                            (mouseState.Y > yCoord) && (mouseState.Y < (yCoord + initialButtonTexture.Height)));

            return result;
        }

        /// <summary>
        /// Allows the game component to update itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        public override void Update(GameTime gameTime)
        {
            clicked = false;

            if (MouseOnButton())
                CheckButtonClick();
   
            base.Update(gameTime);
        }

        /// <summary>
        /// Allows the game component to draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        public override void Draw(GameTime gameTime)
        {
            //left mouse button is not pressed
            if (!pressed)
                //draw the save button using initial save button texture
                spritebatch.Draw(initialButtonTexture, new Rectangle(xCoord, yCoord, initialButtonTexture.Width,
                            initialButtonTexture.Height), Color.White);
            //left mouse button is pressed
            else
                //draw the save button using the pressed state save button texture
                spritebatch.Draw(initialButtonTexture, new Rectangle(xCoord, yCoord, initialButtonTexture.Width,
                            initialButtonTexture.Height), Color.BlanchedAlmond);
            
            base.Draw(gameTime);
        }
    }
}